package org.jeecg.modules.yiktxx.job;

import cn.jpush.api.push.PushResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.JpushConfig;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.yiktxx.entity.HqYiktxx;
import org.jeecg.modules.yiktxx.service.IHqYiktxxService;
import org.jeecg.modules.yiktxx.vo.Yiktxxst;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
public class SyncData implements Job {
    @Autowired
    IHqYiktxxService hqYiktxxService;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    private RedisUtil redisUtil;

    static boolean isFinish = true;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("开始同步一卡通消息");
        if (!isFinish) {
            log.info("上个任务还未完成");
            return;
        }

        isFinish = false;
        List<Yiktxxst> list = null;
        String kah = "";

        List<SysUser> sysUserList = (List<SysUser>)redisUtil.get("SYSUSERLIST");
        if(sysUserList == null) {
            sysUserList = sysUserService.list();
            redisUtil.set("SYSUSERLIST", sysUserList);
        }

        for (SysUser sysUser : sysUserList
        ) {
            kah = sysUser.getUsername();

            try {
                list = hqYiktxxService.getYiktxxlb(kah);
                log.info("查询到, {}条记录", list.size());

            } catch (Exception error) {
                isFinish = true;
                log.error("解析返回报文异常: {}", error.getMessage());
                return;
            }

            if (list != null && list.size() > 0) {
                String finalKah = kah;
                list.stream().forEach(item -> {
                    HqYiktxx hqYiktxx = hqYiktxxService.getOne(new LambdaQueryWrapper<HqYiktxx>()
                            .eq(HqYiktxx::getCreateTime, item.getCreateTime()), false);

                    if (hqYiktxx == null) {
                        hqYiktxx = new HqYiktxx();
                        hqYiktxx.setAccount(item.getAccount());
                        hqYiktxx.setContent(item.getContent());
                        hqYiktxx.setCreateTime(item.getCreateTime());
                        hqYiktxx.setMessagetype(item.getTypeCode());
                        hqYiktxx.setIspush(1);

                        try {
                            PushResult pushResult = JpushConfig.sendPush("一卡通账号消息", hqYiktxx.getContent(), new HashMap<>(), hqYiktxx.getAccount());
                            log.info("极光推送, {}", pushResult.getOriginalContent());

                        } catch (Exception e) {
                            log.error("极光推送异常: {}", e.getMessage());
                            hqYiktxx.setIspush(2);
                        }

                        int rtn = hqYiktxxService.setYid(finalKah, item.getId());
                        log.info("一卡通设置已读, {}", rtn);
                        hqYiktxxService.save(hqYiktxx);
                    }
                });
            }
        }

        isFinish = true;

        log.info("完成同步一卡通消息");
    }
}
